package zhong.Lrc;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.R.string;

public class LrcProcessor {
	
	public ArrayList<Queue> processor(InputStream inputStream){
		ArrayList<Queue> queues=new ArrayList<Queue>();
		Queue<Long> timeMillis=new LinkedList<Long>();
		Queue<String> messages=new LinkedList<String>();
		
		try{
			BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream, "GBK"));
			String temp=null;
//			Pattern pattern=Pattern.compile("\\[([^\\]]+)\\]");
			Pattern pattern=Pattern.compile("\\[(\\d{2}:\\d{2}\\.\\d{2})\\]");
			while((temp=bufferedReader.readLine())!=null){
				Matcher matcher=pattern.matcher(temp);
				if(matcher.find()&&matcher.group().length()==10){
					
					String timeString=matcher.group();
					Long timeMillisLong=time2Long(timeString.substring(1, timeString.length()-1));
					timeMillis.offer(timeMillisLong);
					String msg=temp.substring(10);
					
					messages.add(msg);
				}
				
			}
			

			queues.add(timeMillis);
			queues.add(messages);
			
		}
		
		catch(Exception e){
			e.printStackTrace();
		}
		
		finally{
			try{
				inputStream.close();
			}
			catch(Exception e){
				e.printStackTrace();
			}
		}
		
		return queues;
		
	}
	
	public Long time2Long(String timeString){
		String a[]=timeString.split(":");
		int min=Integer.parseInt(a[0]);
		String b[]=a[1].split("\\.");
		int second=Integer.parseInt(b[0]);
		int millis=Integer.parseInt(b[1]);
		
		
		return min*60*1000+second*1000+millis*10L;
	}

}
